package icraf.carbon.gui.chart;

import icraf.carbon.core.LandCover;
import icraf.carbon.core.LandCoverList;
import icraf.carbon.core.Project;
import icraf.carbon.core.ProjectIO;
import icraf.carbon.core.Zone;
import icraf.carbon.core.Zone.DataType;
import icraf.carbon.gui.LCTableModel;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class PlotDisplayPanel extends JPanel {
	private static final long serialVersionUID = 6872129443097133654L;

	public enum PlotType {
		ROW_TOTAL, COLUMN_TOTAL;
	}

	public static void main(String[] args) {
		Project p = null;
		try {

			p = ProjectIO
					.loadData(new FileReader(new File("c:/degi/Kaltim.car")));
		} catch (IOException e) {
			e.printStackTrace();
		}

		p.updateDataArray();
		PlotDisplayPanel chartPanel = new PlotDisplayPanel(p.getZoneList().get(1), DataType.LANDCOVER_CHANGE, null, PlotType.ROW_TOTAL);
		
		chartPanel.setPreferredSize(new Dimension(600, 400));
		final JFrame frame = new JFrame();
		frame.getContentPane().add(chartPanel, BorderLayout.CENTER);
		frame.pack();
		frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				frame.setVisible(true);
			}
		});
	}

	private LCTableModel table;
	private PlotType plotType;
//	private BarPlot barPlot;
	private Zone zone;
	private DataType dataType;
	private Object attributes;

	public PlotDisplayPanel(Zone zone, DataType dataType, Object attributes, PlotType plotType) {
		super(new BorderLayout());
		this.zone = zone;
		this.dataType = dataType;
		this.attributes = attributes;
		this.plotType = plotType;
		
		// Create example data
//		DataTable data = new DataTable(Integer.class, Double.class);
		LandCoverList lcList = zone.getProject().getLandCoverList();
		int i = 0;
		Double d[] = new Double[lcList.size()];
		String str[] = new String[lcList.size()];
		for(LandCover lc1: lcList) {
			double v = 0;
			String label = "";
			for(LandCover lc2: lcList) {
				if (plotType == PlotType.ROW_TOTAL) {
					v = zone.getValue(lc1, lc2, dataType, attributes);
					label = lc1.getLabel();
				} else if (plotType == PlotType.COLUMN_TOTAL) {
					v = zone.getValue(lc2, lc1, dataType, attributes);
					label = lc2.getLabel();
				}
			}
//			data.add(i, v);
			System.out.println("i="+i + " v="+v);
			d[i] = (double)i;
			str[i] = label;
			i++;
		}
		

	}

	public void showChart(LCTableModel table, PlotType plotType) {
		this.table = table;
		this.plotType = plotType;
		if (plotType == PlotType.ROW_TOTAL) {

		}
	}

	public void getValueTotal() {
		int x = 0;
		double total[] = new double[table.getRowCount() - 1];
		if (table.isTotalFields()) {
			for (int r = 0; r < table.getRowCount() - x; r++) {
				Object v = table.getValueAt(r, table.getColumnCount());
				total[r] = (v != null) ? (Double) v : 0;
			}
		}
		// plot.addBarPlot("Row", total);

	}

	public PlotType getPlotType() {
		return plotType;
	}

	public Zone getZone() {
		return zone;
	}

	public DataType getDataType() {
		return dataType;
	}

	public Object getAttributes() {
		return attributes;
	}

}
